package com.xframe.common.core.service.command;


import com.xframe.domain.BaseModel;
import com.xframe.domain.repository.SysModelRepository;
import com.xframe.domain.role.IUserCredentials;
import com.xframe.domain.sys.SysModel;
import com.xframe.domain.CommandModel;
import com.xframe.common.core.dto.ErrorCode;
import com.xframe.common.core.exception.BusinessException;
import com.xframe.domain.ModelRepositoryFactory;
import com.xframe.xdal.core.exception.DalFrameException;
import com.xframe.xdal.core.model.ConditionValue;
import com.xframe.xdal.core.model.ConditionValueCollection;
import com.xframe.xdal.core.model.SetWrapCollection;
import com.xframe.xdal.core.model.mapping.ITableInfo;
import com.xframe.xdal.core.model.mapping.TableInfoBuilder;
import org.apache.commons.lang3.ObjectUtils;

import java.util.ArrayList;
import java.util.Map;

/**
* @description:模型发布
* @param:
* @return:
* @author: ChaiXinQiang
* @createDate: 16:05 2023/7/24
**/
public class PublishModelProc extends BaseCommand {


    @Override
    public Object ProcCommand(CommandModel requestArgs, IUserCredentials userCredentials) throws BusinessException {

        SysModelRepository sysBaseRepository = ModelRepositoryFactory.builder(SysModel.class);
        try {
            Map<String, Object> dto = requestArgs.getCommandBodyByMap();
            ArrayList<String> modelIds = (ArrayList<String>) dto.get("ids");
            //模型id
            Long modelId = Long.valueOf(modelIds.get(0));
            SysModel sysModel = SysModel.getInstance(Long.valueOf(modelId));
            if (ObjectUtils.isEmpty(sysModel)) {
                throw new DalFrameException(com.xframe.xdal.core.constant.ErrorCode.ARGS_ERROR, "modelId为" + modelId + "的模型不存在或已被删除");
            }

            //创建物理表
            //sysBaseRepository.createTable(sysModel);
            //修改模型状态为1 ： 已发布
            SetWrapCollection setWrapCollection = new SetWrapCollection();
            setWrapCollection.addAutoincrement(BaseModel.VERSION_NAME).
                    add(BaseModel.UPDATEBY_NAME, userCredentials.getName()).
                    addDBexp(BaseModel.UPDATETIME_NAME, "SYSDATE").
                    add("modelState", 1);
            ConditionValueCollection whereCollection = new ConditionValueCollection();
            whereCollection.add(new ConditionValue("modelId", modelId));
            // 根据对象的注解来构造数据表的结构对象
            ITableInfo tableInfo = TableInfoBuilder.BuildTableInfoByAnnotation(SysModel.class);
            //系统实体表 修改 如果成功 修改关联表
            //Integer integer = sysBaseRepository.updateMany(tableInfo, setWrapCollection, whereCollection);
            Integer integer = (int) this.execRepository(sysBaseRepository, "updateMany", tableInfo, setWrapCollection, whereCollection);
            return integer;
        } catch (Exception exception) {
            throw new BusinessException(ErrorCode.INTERNAL_SERVER_ERROR, exception.getMessage());
        }
    }


    @Override
    public String getBeanName() {
        return "Publish";
    }
}
